﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

// ReSharper disable CheckNamespace
namespace System.Security.Cryptography
// ReSharper restore CheckNamespace
{
  
    public class MD5Helper
    {
        public static string Encrypt(string input)
        {
            HashAlgorithm hashAlgorithm = MD5.Create();
            var result = hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(input)).BinaryToHex();
            return result;
        }

        public static string Decrypt(string input)
        {
            if (input == null)
                throw new ArgumentNullException("input");
            input = input.Replace(",", "");
            input = input.Replace("\n", "");
            input = input.Replace("\\", "");
            input = input.Replace(" ", "");
            if (input.Length % 2 != 0)
            {
                input += "20";//空格 
            }
            // 需要将 hex 转换成 byte 数组。 
            var bytes = new byte[input.Length / 2];
            for (int i = 0; i < bytes.Length; i++)
            {
                try
                {
                    // 每两个字符是一个 byte。 
                    bytes[i] = byte.Parse(input.Substring(i * 2, 2),
                    System.Globalization.NumberStyles.HexNumber);
                }
                catch
                {
                    // Rethrow an exception with custom message. 
                    throw new ArgumentException("hex is not a valid hex number!", "input");
                }
            }
            HashAlgorithm hashAlgorithm = MD5.Create();
            return Encoding.UTF8.GetString(bytes);
        }
        /// <summary>
        /// RSA的加密函数
        /// </summary>
        /// <param name="xmlPublicKey">公钥</param>
        /// <param name="encryptString">待加密的字符串</param>
        /// <returns></returns>
        public static string RsaEncrypt(string xmlPublicKey, string encryptString)
        {
            try
            {
                var rsa = new RSACryptoServiceProvider();
                rsa.FromXmlString(xmlPublicKey);
                var plainTextBArray = (new UnicodeEncoding()).GetBytes(encryptString);
                var cypherTextBArray = rsa.Encrypt(plainTextBArray, false);
                var result = Convert.ToBase64String(cypherTextBArray);
                return result;
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        /// <summary>
        /// RSA的解密函数
        /// </summary>
        /// <param name="xmlPrivateKey">私钥</param>
        /// <param name="decryptString">待解密的字符串</param>
        /// <returns></returns>
        public static string RsaDecrypt(string xmlPrivateKey, string decryptString)
        {
            try
            {
                var rsa = new RSACryptoServiceProvider();
                rsa.FromXmlString(xmlPrivateKey);
                var plainTextBArray = Convert.FromBase64String(decryptString);
                var dypherTextBArray = rsa.Decrypt(plainTextBArray, false);
                var result = (new UnicodeEncoding()).GetString(dypherTextBArray);
                return result;
            }
            catch (Exception ex)
            {
                throw;
            }
        } 
    }
}
